package com.examsystem.controller;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.examsystem.pojo.vo.ScoreVo;
import com.examsystem.service.ScoreService;
import com.examsystem.pojo.vo.LoginUser;
import com.examsystem.pojo.vo.Result;
import com.examsystem.pojo.vo.StudentScore;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 86134
 */
@Controller
@RequestMapping("/score")
public class ScoreController {
    @Resource
    private ScoreService scoreService;
    /*
    学生端代码
     */

    /**
     * 获取学生的成绩
     * @return  返回该学生的成绩
     */
    @PreAuthorize("hasAuthority('score:getStudentScore')")
    @RequestMapping("/getStudentScore")
    @ResponseBody
    public String getStudentScore(){
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int userId = user.getUser().getUserId();
        List<StudentScore> userScore = scoreService.getUserScore(userId);
        return JSON.toJSONString(userScore);
    }






    /*
    教师端代码
     */

    /**
     * 获取考试情况
     * @param choice 【1：查看单独班级的学生情况  2：参加此次考试的所有班级的average排名 】
     * @param classId 班级id
     * @param paperId 试卷id
     * @return  result R对象
     */
//    @PreAuthorize("hasAuthority('teacher')")
    @RequestMapping("/showScore/{choice}/{classId}/{paperId}")
    @ResponseBody
    public Result showScore(@PathVariable Integer choice,@PathVariable Integer classId,@PathVariable Integer paperId){
        List<ScoreVo> scores = scoreService.showScore(choice, classId, paperId);
        return Result.succ(scores);
    }
}
